﻿<UserControl x:Class="Commanding.Modules.Order.Views.OrdersEditorView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:models="clr-namespace:Commanding.Modules.Order.PresentationModels"
    xmlns:converter="clr-namespace:Commanding.Modules.Order.Converter"
    Width="Auto" Height="Auto" >
    <UserControl.Resources>
        <ControlTemplate x:Key="ErrorTemplate">
        <Border BorderBrush="Red" BorderThickness="1" Padding="2" CornerRadius="2">
            <AdornedElementPlaceholder />
        </Border>
        </ControlTemplate>
        <converter:ErrorConverter x:Key="errorConverter"/>
        <converter:DateConverter x:Key="dateConverter"/>
        <converter:StringToNullableNumberConverter x:Key="stringToNullableNumberConverter" />
        <Style x:Key="TextBoxInError" TargetType="{x:Type TextBox}">
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Margin" Value="0,5,0,5"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="HorizontalContentAlignment" Value="left"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Border x:Name="Border" Background="{DynamicResource WindowBackgroundBrush}" BorderBrush="{DynamicResource SolidBorderBrush}" BorderThickness="1" Padding="2" CornerRadius="2">
                                <ScrollViewer Margin="0" x:Name="PART_ContentHost" Style="{DynamicResource SimpleScrollViewer}" Background="{TemplateBinding Background}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Validation.HasError" Value="true">
                                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource errorConverter}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate DataType="{x:Type models:OrderPresentationModel}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition  Width="*"/>
                </Grid.ColumnDefinitions>
                <Label AutomationProperties.AutomationId="OrderNameLabel" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding OrderName}"  HorizontalContentAlignment="Left" FontWeight="Bold"/>
                <Label AutomationProperties.AutomationId="DateLabel" Grid.Row="1" Grid.Column="0">Delivery Date</Label>
                <Label AutomationProperties.AutomationId="QuantityLabel" Grid.Row="2" Grid.Column="0">Quantity</Label>
                <Label AutomationProperties.AutomationId="PriceLabel" Grid.Row="3" Grid.Column="0">Price</Label>
                <Label AutomationProperties.AutomationId="ShippingLabel" Grid.Row="4" Grid.Column="0">Shipping</Label>
                <Label AutomationProperties.AutomationId="TotalLabel" Grid.Row="5" Grid.Column="0">Total</Label>
                <TextBox AutomationProperties.AutomationId="DeliveryDateTextBox" Grid.Row="1" Grid.Column="1" Text="{Binding DeliveryDate, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True, Converter={StaticResource dateConverter}}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Style="{StaticResource TextBoxInError}" />
                <TextBox AutomationProperties.AutomationId="QuantityTextBox" Grid.Row="2" Grid.Column="1" Text="{Binding Quantity, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True, Converter={StaticResource stringToNullableNumberConverter}}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Style="{StaticResource TextBoxInError}" />
                <TextBox AutomationProperties.AutomationId="PriceTextBox" Grid.Row="3" Grid.Column="1" Text="{Binding Price, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True, Converter={StaticResource stringToNullableNumberConverter}}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Style="{StaticResource TextBoxInError}" />
                <TextBox AutomationProperties.AutomationId="ShippingTextBox" Grid.Row="4" Grid.Column="1" Text="{Binding Shipping, Converter={StaticResource stringToNullableNumberConverter}}" />
                <TextBox AutomationProperties.AutomationId="TotalTextBox" Grid.Row="5" Grid.Column="1" IsReadOnly="True"  Text="{Binding Total, Mode=OneWay}" />
                <Button AutomationProperties.AutomationId="SaveButton" Grid.Row="6" Grid.Column="1" Content="Save" Command="{Binding SaveOrderCommand}"></Button>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".4*"/>
        <ColumnDefinition  Width=".6*"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" CornerRadius="4,4,4,4" BorderBrush="#193441" Background="#FCFFF5" BorderThickness="2,2,2,2" Margin="5" Padding="5">
        <ListView AutomationProperties.AutomationId="OrderListView" ItemsSource="{Binding Orders}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" Width="Auto" Height="Auto" >
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type models:OrderPresentationModel}">
                    <TextBlock Text="{Binding OrderName}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Border>
    <Border Grid.Column="1" CornerRadius="4,4,4,4" BorderBrush="#193441" Background="#FCFFF5" BorderThickness="2,2,2,2" Margin="5" Padding="5">
        <ContentControl Content="{Binding SelectedOrder}" Width="Auto" Height="Auto"/>
    </Border>
    </Grid>
</UserControl>
